/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* InputView.java
*
* Created on November 29, 2007, 9:09 PM
 */
package gui.genomemap.view.inputView;

import genomemap.data.OrderElement;

import gui.genomemap.event.event.WorkerInputEvent;
import gui.genomemap.event.event.WorkerInputListener;
import gui.genomemap.inputModel.InputModel;
import gui.genomemap.view.View;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.swing.event.EventListenerList;

/**
 *
 * @author  stewari1
 */
public class InputView extends javax.swing.JPanel {


    // @todo we may replace the listener list by a worker but that may be overkill

    /** Field description */
    private final EventListenerList workerListenerList = new EventListenerList();

    /** Field description */
    private InputModel inputModel = null;

    /** Field description */
    private View view = null;

    /**
     * Creates new form InputView
     */
    InputView() {
        initComponents();
    }

    void referesh() {}

    public static void main(String[] args) {

        Set<Integer> intSet = new HashSet<>();

        intSet.add(new Integer(1));
        intSet.add(new Integer(2));
        intSet.add(new Integer(3));
        intSet.add(new Integer(4));

        Integer[] intArray = intSet.toArray(new Integer[1]);

        for (int index = 0; index < intArray.length - 1; index++) {

            System.out.println("array length: " + intArray.length);
        }

        System.out.println("array length: " + intArray.length);
    }

    public static String createOrderString(LinkedHashSet<OrderElement> orderSet) {

        String orderStr       = "";
        OrderElement[] orders = orderSet.toArray(new OrderElement[1]);

        for (int index = 0; index < orders.length; index++) {

            orderStr += orders[index].getVal() + ",";

            if (index == orders.length - 1) {
                orderStr += orders[orders.length - 1].getVal();
            }
        }

        System.out.println("I`m fine");

        return orderStr;
    }

    void setOrder(Collection<OrderElement> orderSet) {
        setOrderString(OrderElement.createOrderString(orderSet));
    }

    void setOrderString(String orderString) {}

    public void addWorkerInputListener(WorkerInputListener l) {
        workerListenerList.add(WorkerInputListener.class, l);
    }

    public void removeWorkerInputListener(WorkerInputListener l) {
        workerListenerList.remove(WorkerInputListener.class, l);
    }

    void fireWorkerInputEvent(WorkerInputEvent e) {

        if (e == null) {
            return;
        }

        Object[] listeners = workerListenerList.getListenerList();


        // LIFO for (int i = listeners.length-2; i>=0; i-=2) {
        // FIFO strategy
        for (int i = 0; i <= listeners.length - 2; i += 2) {

            if (listeners[i] == WorkerInputListener.class) {

                switch (e.getType()) {

                    case WorkerInputEvent.COM_GLHOOD :
                        ((WorkerInputListener) listeners[i + 1]).doComGLHoodInput(e);

                        break;

                    case WorkerInputEvent.COM_PLHOOD :
                        ((WorkerInputListener) listeners[i + 1]).doComPLHoodInput(e);

                        break;

                    case WorkerInputEvent.COM_JLHOOD :
                        ((WorkerInputListener) listeners[i + 1]).doComJLHoodInput(e);

                        break;

                    case WorkerInputEvent.ANNEAL :
                        ((WorkerInputListener) listeners[i + 1]).doAnnealInput(e);

                        break;

                    case WorkerInputEvent.PROG_CURVE :
                        ((WorkerInputListener) listeners[i + 1]).doProgCurveInput(e);

                        break;
                }
            }
        }
    }

    InputModel getInputModel() {
        return inputModel;
    }

    /**
     * to be overridden in the subclass
     */
    public void setEnabledRunModel(boolean enabled) {}

    public void setInputModel(InputModel workerInputModel) {

        this.inputModel = workerInputModel;

        referesh();
    }

    View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(
                0, 400, Short.MAX_VALUE));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(
                0, 300, Short.MAX_VALUE));

    }    // </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
